<?php 
	require "./include/common.inc.php";

    $id = $_COOKIE['uid'];
    $min_path = WEB.'uploads/min_teacher/';
	$sql  = "select `name`,`avatar` from `teacher` where `id`=$id";
    $user = get_one($sql);

    if( $_POST ){
	  	//验证，不要轻易相信经过客户端的任何数据
	  	$identity = htmlspecialchars($_POST['identity']);  //被留言者身份[表名]
	  	$target   = htmlspecialchars($_POST['target']);    //被留言者[姓名]
	  	$title    = htmlspecialchars($_POST['title']);     //留言标题
	  	$content  = htmlspecialchars($_POST['content']);   //留言内容
	  	$time     = time();      //留言时间
	  	$from     = 'student';   //留言者身份
	  	// $from_id  = $id;         //留言者id

	  	if( $target ){
	  	    $sql = "select `id` from `$identity` where `name`='$target'";
	  	    $res = mysqli_query($conn,$sql);
	  	    $target_id = mysqli_fetch_assoc($res)['id'];
	  	}else{
	  	    showMsg('目标不存在！请确认对方姓名!');
	  	}
	  	$sql = "INSERT INTO `message` (`from`,`from_id`,`to`,`to_id`,`title`,`content`,`addtime`,`status`) VALUES ('$from','$id','$identity','$target_id','$title','$content','$time',0)";
	  	//echo $sql;die;
	  	$res = mysqli_query($conn,$sql);
	  	if( $res && mysqli_insert_id($conn) ){
	  		showMsg('留言成功!','teacher_index.php');
	  	}else{
	  		showMsg('留言失败!');
	  	}
    }

?>

<!DOCTYPE html>
<html>
<head>
	<title>用户绑定</title>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<!-- loading mui -->
	<link rel="stylesheet" type="text/css" href="css/mui.min.css">
	<!-- loading mui.picker.min.css -->
	<link rel="stylesheet" type="text/css" href="css/mui.picker.min.css">
	<!-- loading mui.poppicker.css -->
	<link rel="stylesheet" type="text/css" href="css/mui.poppicker.css">
	<!-- custorm style -->
	<link rel="stylesheet" type="text/css" href="css/style.css">
	<!-- 引入核心js脚本 -->
	<script src="include/ue/ueditor.config.js"></script>
    <script src="include/ue/ueditor.all.min.js"> </script>
    <script src="include/ue/lang/zh-cn/zh-cn.js"></script>
	<style>
	form label,form input,form textarea{
		font-size: 12px!important;
	}
	</style>
</head>
<body>
	<!-- 导航栏 -->
	<header id="header" class="mui-bar mui-bar-nav">
		<h1 class="mui-title">奇猿学生课程管理系统</h1>
		<a class="mui-action-back mui-btn mui-btn-blue mui-btn-link mui-btn-nav mui-pull-left" href="javascript:history.go(-1)"><span class="mui-icon mui-icon-left-nav"></span></a>
		<a class="mui-icon mui-icon-bars mui-pull-right" href="#topPopover"></a>
	</header>
	<!-- 右上角弹出菜单 -->
	<div id="topPopover" class="mui-popover">
		<div class="mui-popover-arrow"></div>
		<div class="mui-scroll-wrapper">
			<div class="mui-scroll">
				<ul class="mui-table-view">
					<li class="mui-table-view-cell"><a href="teacher_index.php">首页</a>
					</li>
					<li class="mui-table-view-cell"><a href="query.html">我要查询</a>
					</li>
					<li class="mui-table-view-cell"><a href="rate.html">我要评价</a>
					</li>
					<li class="mui-table-view-cell"><a href="enroll.php">我要选课</a>
					</li>						
					<li class="mui-table-view-cell"><a href="teacher_center.php">个人中心</a>
					</li>
					<li class="mui-table-view-cell"><a href="login.php">退出登录</a>
					</li>

				</ul>
			</div>
		</div>
	</div>
	<!-- 内容 -->
	<div class="content">
		<div class="userBind">
			<!-- 头像 -->
			<div class="head">
				<div class="head_icon"><img src="<?=$user['avatar']; ?>"></div>
				<p><?=$user['name']; ?></p>
			</div>
			<!-- 用户注册内容 -->
			<form class="mui-input-group" method="post">
					<div class="mui-input-row">
						<label>标题:</label>
						<input type="text" name="title" class="mui-input-clear" placeholder="请输入留言标题" data-input-clear="5"><span class="mui-icon mui-icon-clear mui-hidden"></span>
					</div>
					<div class="mui-input-row">
						<label>对方身份:</label>
						<label id="userResult" class="shenfen">学生</label>
						<input type="hidden" name="identity" value="student">
						<input type="button" class="mui-btn mui-btn-block" data-input-clear="5" id="showUserPicker" value="选择对方身份">
					</div>
					<div class="mui-input-row">
						<label>对方昵称:</label>
						<input type="text" name="target" class="mui-input-clear" placeholder="请输入被留言者的姓名" data-input-clear="5"><span class="mui-icon mui-icon-clear mui-hidden"></span>
					</div>
					<textarea id="ue" name="content" rows="8" placehoder=请输入留言内容></textarea>
				<input type="submit" class="mui-btn mui-btn-success mui-btn-block upload" value="提交">
				</form>
		</div>

	</div>
	<!-- loading jquery -->
	<script type="text/javascript" src="js/jquery-1.12.1.min.js"></script>
	<!-- loading mui.min.js -->
	<script type="text/javascript" src="js/mui.min.js"></script>
	<!-- loading mui.picker.min.js -->
	<script type="text/javascript" src="js/mui.picker.min.js"></script>
	<!-- loading mui.poppicker.js -->
	<script type="text/javascript" src="js/mui.poppicker.js"></script>
	<script type="text/javascript">
		(function($, doc) {
			$.init();
			$.ready(function() {
				//普通示例
				var userPicker = new $.PopPicker();
				userPicker.setData([{
					value: 'student',
					text: '学生'
				}, {
					value: 'teacher',
					text: '老师'
				}]);
				var showUserPickerButton = doc.getElementById('showUserPicker');
				var userInput = doc.querySelector('input[name=identity]');
				var userResult = doc.getElementById('userResult');
				showUserPickerButton.addEventListener('tap', function(event) {
					userPicker.show(function(items) {
						userResult.innerText = items[0].text;
						userInput.value = items[0].value;
						//返回 false 可以阻止选择框的关闭
						//return false;
					});
				}, false);
			});
		})(mui, document);
		$(function  () {
			//获取短信验证码
			var validCode=true;
			$(".msgs").click (function  () {
				var time=60;
				var code=$(this);
				if (validCode) {
					validCode=false;
					code.addClass("msgs1");
					var t=setInterval(function  () {
						time--;
						code.html(time+"秒");
						if (time==0) {
							clearInterval(t);
						code.html("重新获取");
							validCode=true;
						code.removeClass("msgs1");
						}
					},1000)
				}
			})
		});

		var ue = UE.getEditor('ue',{toolbars: [[
            'fullscreen', 'source', '|', 'undo', 'redo', '|',
            'bold', 'italic', 'underline', 'fontborder', 'strikethrough', 'superscript', 'subscript', 'removeformat', 'formatmatch', 'autotypeset', 'blockquote', 'forecolor', 'backcolor', 'indent', '|',
            'link', 'unlink','help'
	        ]]
	        ,initialFrameWidth:780  //初始化编辑器宽度,默认1000
	    	,initialFrameHeight:100  //初始化编辑器高度,默认320
    	});
	</script>
	<script src="./js/img.js"></script>
</body>
</html>